import threading


class ThreadManager:
    """
    多线程管理类
    """

    def __init__(self):
        """
        初始化方法，创建线程列表和进程锁
        """
        self.threads = []
        self.lock = threading.Lock()

    # 创建新线程
    def create_thread(self, target, args=()):
        """
        创建新线程

        参数:
        target (function): 线程要执行的目标函数
        args (tuple): 目标函数的参数，默认为空元组
        """
        thread = threading.Thread(target=target, args=args)
        # thread.start()
        self.threads.append(thread)

    # 等待所有线程完成
    def wait_all_threads(self):
        """
        等待所有线程完成
        """
        for thread in self.threads:
            thread.join()

    # 检查指定线程是否仍在运行
    def is_thread_alive(self, thread):
        """
        检查指定线程是否仍在运行

        参数:
        thread (threading.Thread): 要检查的线程

        返回:
        bool: 如果线程仍在运行，返回 True，否则返回 False
        """
        return thread.is_alive()

    # 获取进程锁
    def acquire_lock(self):
        """
        获取进程锁
        """
        self.lock.acquire()

    # 释放进程锁
    def release_lock(self):
        """
        释放进程锁
        """
        self.lock.release()

    # 启动所有线程
    def start_all_threads(self):
        """
        启动所有已创建的线程
        """
        for thread in self.threads:
            thread.start()
